"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

require("antd/lib/button/style/css");

var _button = _interopRequireDefault(require("antd/lib/button"));

require("antd/lib/checkbox/style/css");

var _checkbox = _interopRequireDefault(require("antd/lib/checkbox"));

require("antd/lib/row/style/css");

var _row = _interopRequireDefault(require("antd/lib/row"));

require("antd/lib/date-picker/style/css");

var _datePicker = _interopRequireDefault(require("antd/lib/date-picker"));

require("antd/lib/input/style/css");

var _input = _interopRequireDefault(require("antd/lib/input"));

require("antd/lib/col/style/css");

var _col = _interopRequireDefault(require("antd/lib/col"));

require("antd/lib/icon/style/css");

var _icon = _interopRequireDefault(require("antd/lib/icon"));

require("antd/lib/form/style/css");

var _form = _interopRequireDefault(require("antd/lib/form"));

var _react = _interopRequireWildcard(require("react"));

var _icons = require("@ant-design/icons");

var _classnames = _interopRequireDefault(require("classnames"));

var _moment = _interopRequireDefault(require("moment"));

var _selectCustom = _interopRequireDefault(require("../../input/selectCustom"));

var _utils = require("../../../utils/utils");

var _events = _interopRequireDefault(require("../../../utils/events"));

var _util = require("../util");

var _filter = _interopRequireDefault(require("./filter.less"));

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

var FormItem = _form.default.Item;
var formItemLayout = {
  labelCol: {
    span: 8
  },
  wrapperCol: {
    span: 16
  }
};
var formItemLayoutDate = {
  labelCol: {
    span: 6
  },
  wrapperCol: {
    span: 17
  },
  style: {
    position: 'relative',
    left: '-12.8%',
    width: '115%'
  }
}; // const { MonthPicker } = DatePicker;

var Filter = /*#__PURE__*/function (_PureComponent) {
  _inherits(Filter, _PureComponent);

  var _super = _createSuper(Filter);

  function Filter(props) {
    var _this;

    _classCallCheck(this, Filter);

    _this = _super.call(this, props);

    _this.onOpenClick = function () {
      _this.setState(function (preState) {
        return {
          open: !preState.open
        };
      });
    };

    _this.onAdvancedSearchSubmit = function () {
      var onSearch = _this.props.onSearch;

      var values = _this.getValues();

      onSearch(values);
    };

    _this.onAdvancedSearchReset = function () {
      var onReset = _this.props.onReset;

      _this.formRef.current.resetFields(); // const values = this.getValues();


      var parms = {};
      onReset && onReset(parms);
    };

    _this.getShowColumnsFilter = function () {
      var _this$state = _this.state,
          open = _this$state.open,
          columnsFilter = _this$state.columnsFilter;

      var numObj = _this.num().clear();

      var max = 18; // 计数到18即为一行

      var columns = columnsFilter.filter(function (i) {
        return i.show;
      });

      if (open) {
        return columns;
      } else {
        var list = []; // 待显示的列表

        for (var i = 0; i < columns.length; i += 1) {
          numObj.add(columns[i]);

          if (columns[i].show && numObj.getNum() <= max) {
            list.push(columns[i]);
          }
        }

        return list;
      }
    };

    _this.reFlush = function () {
      var columnsFilter = _this.props.columnsFilter;

      _this.setState({
        columnsFilter: (0, _util.getList)(columnsFilter)
      });
    };

    _this.trim = function (e) {
      var value = String(e.target.value).trim();
      return value;
    };

    _this.labelRender = function (item) {
      if (item.labelRender) {
        // Render优先
        return item.labelRender;
      }

      var text = item.label;

      if (text && text.length > 5) {
        // 让他小一点
        return /*#__PURE__*/_react.default.createElement("span", {
          style: {
            fontSize: '10px'
          }
        }, text);
      }

      return text;
    };

    _this.num = function () {
      var mas = 0;
      return {
        clear: function clear() {
          mas = 0;
          return this;
        },
        add: function add(item) {
          if (item.inputType === 'dataRange') {
            mas += 12;
          } else if (item.inputType === 'checkBox') {
            mas += 3;
          } else {
            mas += 6;
          }

          return mas;
        },
        getNum: function getNum() {
          return mas;
        }
      };
    };

    _this.getValues = function () {
      // const state = this.state;
      var columnsFilter = _this.state.columnsFilter;
      var _this$props = _this.props,
          submitPreMiddleware = _this$props.submitPreMiddleware,
          _this$props$defaultCo = _this$props.defaultConditon,
          defaultConditon = _this$props$defaultCo === void 0 ? {} : _this$props$defaultCo;

      var values = _this.formRef.current.getFieldsValue();

      var valuesKey = Object.keys(values);

      for (var i = 0; i < valuesKey.length; i += 1) {
        if (values[valuesKey[i]] && values[valuesKey[i]].valueOf) {
          // 日期转化为时间戳
          values[valuesKey[i]] = values[valuesKey[i]].valueOf();
        }
      } // 日期输入反了，自动给他调过开


      columnsFilter.forEach(function (i) {
        if (i.inputType === 'dataRange' && i.value.length && i.value.length === 2) {
          // 开始调整日期
          if (values[i.value[0]] && values[i.value[1]] && Number(values[i.value[0]]) > Number(values[i.value[1]])) {
            var _this$formRef$current;

            var a = values[i.value[0]];
            values[i.value[0]] = values[i.value[1]];
            values[i.value[1]] = a;

            _this.formRef.current.setFieldsValue((_this$formRef$current = {}, _defineProperty(_this$formRef$current, i.value[0], (0, _moment.default)(new Date(values[i.value[0]]))), _defineProperty(_this$formRef$current, i.value[1], (0, _moment.default)(new Date(values[i.value[1]]))), _this$formRef$current));
          }
        }
      });

      if (submitPreMiddleware) {
        // 暴露一个外部可以处理参数的接口
        values = submitPreMiddleware(values);
      }

      return _objectSpread(_objectSpread({}, defaultConditon), values);
    };

    _this.state = {
      open: false,
      columnsFilter: (0, _util.getList)(props.columnsFilter)
    };
    _this.formRef = _react.default.createRef();
    return _this;
  }

  _createClass(Filter, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      var pathname = window.location.pathname;

      _events.default.addListener("".concat(pathname, "=refresh"), function () {
        var props = _this2.props;

        _this2.setState({
          columnsFilter: (0, _util.getList)(props.columnsFilter)
        });
      });
    }
  }, {
    key: "UNSAFE_componentWillReceiveProps",
    value: function UNSAFE_componentWillReceiveProps(nextProps) {
      /* ! 千万不要每次都传新的，会卡死的   用useMome  */

      /*
       * listArr 这个数组是不确定什么时候从后台拉取成功了，所以，这就是该行代码的意义
       * */
      if (nextProps.columnsFilter !== this.props.columnsFilter) {
        this.setState({
          columnsFilter: (0, _util.getList)(nextProps.columnsFilter)
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var props = this.props;
      var _this$state2 = this.state,
          open = _this$state2.open,
          columnsFilter = _this$state2.columnsFilter;
      var _this$props$defaultCo2 = this.props.defaultConditon,
          defaultConditon = _this$props$defaultCo2 === void 0 ? {} : _this$props$defaultCo2;
      var list = this.getShowColumnsFilter();
      var numObj = this.num().clear();
      var showList = columnsFilter.filter(function (item) {
        return item.show;
      });
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)('filter', _filter.default.filter, _filter.default.content)
      }, /*#__PURE__*/_react.default.createElement("div", {
        style: _objectSpread({
          display: 'none',
          position: 'absolute',
          top: '-10px',
          right: '0px',
          cursor: 'pointer'
        }, props.iconStyle)
      }, /*#__PURE__*/_react.default.createElement(_icon.default, {
        onClick: this.onClickMenu,
        type: "setting"
      })), /*#__PURE__*/_react.default.createElement(_form.default, {
        ref: this.formRef
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: _filter.default.dashed
      }, /*#__PURE__*/_react.default.createElement(_row.default, {
        gutter: 24
      }, list.map(function (i, index) {
        numObj.add(i);
        var content = false;

        if (i.render) {
          content = /*#__PURE__*/_react.default.createElement(_col.default, {
            // style={{ height: '48px' }}
            span: i.inputType === 'dataRange' ? 12 : 6,
            key: i.label
          }, i.render(FormItem, i.inputType === 'dataRange' ? formItemLayoutDate : formItemLayout, defaultConditon));
        } else if (i.inputType === 'code') {
          content = /*#__PURE__*/_react.default.createElement(_col.default, {
            span: 6,
            key: i.label
          }, /*#__PURE__*/_react.default.createElement(FormItem, _extends({}, formItemLayout, {
            label: _this3.labelRender(i),
            name: i.value,
            initialValue: defaultConditon[i.value],
            getValueFromEvent: inputContract
          }), /*#__PURE__*/_react.default.createElement(_input.default, {
            placeholder: "\u8BF7\u8F93\u5165"
          })));
        } else if (i.inputType === 'text') {
          content = /*#__PURE__*/_react.default.createElement(_col.default, {
            span: 6,
            key: i.label
          }, /*#__PURE__*/_react.default.createElement(FormItem, _extends({}, formItemLayout, {
            label: _this3.labelRender(i),
            name: i.value,
            initialValue: defaultConditon[i.value],
            getValueFromEvent: _utils.inputTrim
          }), /*#__PURE__*/_react.default.createElement(_input.default, {
            placeholder: "\u8BF7\u8F93\u5165"
          })));
        } else if (i.inputType && i.inputType.split(',')[0] === 'select') {
          content = /*#__PURE__*/_react.default.createElement(_col.default, {
            span: 6,
            key: i.label
          }, /*#__PURE__*/_react.default.createElement(FormItem, _extends({}, formItemLayout, {
            label: _this3.labelRender(i),
            name: i.value,
            initialValue: defaultConditon[i.value]
          }), /*#__PURE__*/_react.default.createElement(_selectCustom.default, {
            optionList: i.listArr || [],
            keyValue: i.keyValue || ['name', 'id'],
            mode: i.inputType.split(',')[1] || '' // extraObject={{
            //   id: currentItem.cooperativePartnerId,
            //   name: currentItem.cooperativePartnerName,
            // }}

          })));
        }
        /* else if (i.inputType === 'cascaderProduct') {
        content = (
          <Col span={6} key={i.label}>
            <FormItem {...formItemLayout} label={this.labelRender(i)}>
              {getFieldDecorator(i.value, {
                initialValue: defaultConditon[i.value],
              })(
                <ProductCategoryCascader
                  productList={i.listArr || props.preThis.props[props.type][i.listName]}
                />
              )}
            </FormItem>
          </Col>
        );
        } */
        else if (i.inputType === 'dataRange') {
            content = /*#__PURE__*/_react.default.createElement(_col.default, {
              span: 12,
              key: i.label,
              style: {
                overflow: 'hidden'
              }
            }, /*#__PURE__*/_react.default.createElement(FormItem, _extends({}, formItemLayoutDate, {
              label: _this3.labelRender(i)
            }), /*#__PURE__*/_react.default.createElement(_row.default, null, /*#__PURE__*/_react.default.createElement(_col.default, {
              span: 11
            }, /*#__PURE__*/_react.default.createElement(FormItem, {
              name: i.value[0],
              initialValue: defaultConditon[i.value[0]] ? (0, _moment.default)(new Date(defaultConditon[i.value[0]])) : undefined
            }, /*#__PURE__*/_react.default.createElement(_datePicker.default, {
              style: {
                width: '100%'
              },
              format: "YYYY/MM/DD"
            }))), /*#__PURE__*/_react.default.createElement(_col.default, {
              span: 2,
              style: {
                textAlign: 'center',
                display: 'flex',
                justifyContent: 'center',
                alignItems: 'center'
              }
            }, "~"), /*#__PURE__*/_react.default.createElement(_col.default, {
              span: 11
            }, /*#__PURE__*/_react.default.createElement(FormItem, {
              name: i.value[1],
              initialValue: defaultConditon[i.value[1]] ? (0, _moment.default)(new Date(defaultConditon[i.value[1]])) : undefined
            }, /*#__PURE__*/_react.default.createElement(_datePicker.default, {
              style: {
                width: '100%'
              },
              format: "YYYY/MM/DD"
            }))))));
          } else if (i.inputType === 'checkBox') {
            content = /*#__PURE__*/_react.default.createElement(_col.default, {
              span: 3,
              style: {
                paddingLeft: 0,
                paddingRight: 0,
                position: 'relative',
                left: '3%'
              }
            }, /*#__PURE__*/_react.default.createElement(FormItem, {
              valuePropName: "checked",
              name: i.value,
              initialValue: defaultConditon[i.value]
            }, /*#__PURE__*/_react.default.createElement(_checkbox.default, null, i.label)));
          }

        if (numObj.getNum() === 24 || numObj.getNum() === 21 || numObj.getNum() === 18 && list[index + 1] && list[index + 1].inputType === 'dataRange') {
          numObj.clear();
          return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, content, /*#__PURE__*/_react.default.createElement(_col.default, {
            span: 24
          }));
        }

        return content;
      }), /*#__PURE__*/_react.default.createElement(_col.default, {
        className: _filter.default.textRight,
        style: {
          float: 'right',
          marginTop: '-1px',
          marginLeft: 'auto',
          marginBottom: 12
        }
      }, /*#__PURE__*/_react.default.createElement(_button.default, {
        style: {
          marginRight: '8px'
        },
        type: "primary",
        onClick: this.onAdvancedSearchSubmit,
        htmlType: "submit"
      }, "\u67E5\u8BE2"), /*#__PURE__*/_react.default.createElement(_button.default, {
        onClick: this.onAdvancedSearchReset
      }, "\u91CD\u7F6E"), /*#__PURE__*/_react.default.createElement("a", {
        style: {
          marginLeft: '8px',
          display: showList.length <= 2 ? 'none' : ''
        },
        onClick: this.onOpenClick
      }, open ? '收起' : '展开', open ? /*#__PURE__*/_react.default.createElement(_icons.UpOutlined, {
        style: {
          marginLeft: '4px'
        }
      }) : /*#__PURE__*/_react.default.createElement(_icons.DownOutlined, {
        style: {
          marginLeft: '4px'
        }
      })))))));
    }
  }]);

  return Filter;
}(_react.PureComponent);

var _default = Filter;
exports.default = _default;